package com.riant.controller;

import com.riant.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class HelloController {

    @RequestMapping(path = "/v1/index1", method = RequestMethod.GET)
    public String index() {

        return "index";
    }

    @RequestMapping(path = "/v1/index2", method = RequestMethod.GET)
    public ModelAndView index2(Model model) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index2");
        User user = new User();
        user.setAge((short)10);
        user.setName("张三");
        user.setId(01);
//        model.addAllAttributes(user);
        modelAndView.addObject(user);
        return  modelAndView;
    }

    @RequestMapping(path = "/v1/index3", method = RequestMethod.GET)
    public String index3(Model model) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("index2");
//        User user = new User();
//        user.setAge((short)10);
//        user.setName("张三");
//        user.setId(01);
////        model.addAllAttributes(user);
//        modelAndView.addObject(user);
        return  "forward:/templates/index3.html";
    }

    @RequestMapping(path = "/v1/index4", method = RequestMethod.GET)
    public String index4(Model model) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("index2");
//        User user = new User();
//        user.setAge((short)10);
//        user.setName("张三");
//        user.setId(01);
////        model.addAllAttributes(user);
//        modelAndView.addObject(user);
        return  "redirect:index4";
    }

    @RequestMapping(path = "/v1/index5", method = RequestMethod.GET)
    public String index5(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //response.sendRedirect()可以重定向到相对路径地址，且重定向地址会变
        return  "redirect:https://www.hao123.com/";
//        response.sendRedirect("https://www.hao123.com/");
    }

    @RequestMapping(path = "/v1/index6", method = RequestMethod.GET)
    public void index6(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //response.sendRedirect()可以重定向到相对路径地址，且重定向地址会变
        response.sendRedirect("https://www.hao123.com/");
    }

    @RequestMapping(path = "/v1/index7", method = RequestMethod.GET)
    public String index7(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //response.sendRedirect()可以重定向到相对路径地址，且重定向地址会变

        return  "forward:https://www.hao123.com/";
    }
}
